<template>
    <div class="app-container">
        <div class="warp_padding"> 
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">学年: </label>
                <el-date-picker
                  v-model="formDate.termYear"
                  type="year"
                  placeholder="选择年" value-format="yyyy" @change="toQuery()" style="width: 100px;">
                </el-date-picker>
                
                <span v-if="device!=='mobile'">    
                    <label class="labelQuery">状态: </label>
                    <el-select v-model="formDate.status" clearable placeholder="学段搜索" @change="toQuery()" style="width: 130px;">
                        <el-option
                              v-for="item in schcut"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>
                </span>

                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['term_all','term_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                  <eForm ref="form" :is-add="isAdd" :phname="phname" :schcut="schcut"/>
                </div>
            </div>

            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>
                
                <el-table-column prop="termYear" label="学年"  sortable :show-overflow-tooltip="true"></el-table-column>

                <el-table-column prop="termSegmentName" label="学期"  sortable :show-overflow-tooltip="true"></el-table-column>  

                <el-table-column prop="status" label="状态" >
                     <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.status =='0'">启用</el-tag>
                        <el-tag type="info" v-if="scope.row.status =='1'">禁用</el-tag>
                    </template>
                </el-table-column>


                <el-table-column  label="入学时间" prop="starttime" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <div>{{scope.row.starttime | formatDate}} </div>
                    </template>
                </el-table-column>

                
                <el-table-column  label="结束时间" prop="endtime" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <div>{{scope.row.endtime | formatDate}} </div>
                    </template>
                </el-table-column>


                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button size="mini" type="success" v-if="checkPermission(['term_all','term_edit'])" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" v-if="checkPermission(['term_all','term_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" v-if="checkPermission(['sch_all','sch_del'])" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>

    </div>
</template>

<script>



import checkPermission from '@/utils/permission' 
import {formatDate} from '@/utils/formatDate.js';
import eForm from './module/form'
import { termList, termDel } from '@/api/request'
import { mapGetters } from 'vuex'
export default {
    components: { eForm },
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    data() {
        return {
            phname:[],
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                status:'',
                termYear:'',
            },
            isAdd:true,
            schcut: [
                {
                    id: 0,
                    name: '启用'
                },
                {
                    id: 1,
                    name: '禁用'
                },
                
            ],
         }
    },
    filters: {
            formatDate(time) {
                let date = new Date(time);
                return formatDate(date, 'yyyy-MM-dd');
            }, 
    },
    methods: {
        checkPermission,
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        add(){
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                termYear:'',
                status: '0',
                endtime:'',
                starttime:'',
                termSegment:'',     
            },
            this.$refs.form.dialog = true
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            termList(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        
        handleEdit(index, row) {
            this.isAdd = false
            this.$refs.form.dialog = true
            let Resuts = Object.assign({}, row) 
            this.$refs.form.form = Resuts
        },
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        termDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) {
                                console.log("满足")
                                this.formDate.pageNo = this.formDate.pageNo -1
                            }
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        termDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) {
                                this.formDate.pageNo = this.formDate.pageNo -1
                            }
                            this.init();
                        })

                    }).catch(() => {
            });
        },

        
    },
    mounted:function(){
        this.init()
    },
}

</script>


<style scoped lang="scss">

</style>
